local skel = fk.CreateSkill {
  name = "lb_zero__wanwo",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable {
  ["lb_zero__wanwo"] = "万我",
  [":lb_zero__wanwo"] = "<b>锁定技</b>，每轮开始时或你受到伤害后，你获得一个<a href=':lb_zero__xiqu'><font color='red'>〖锡躯〗</font></a>。每轮结束后或你造成伤害后，你失去一个可用〖锡躯〗，使用一张所有角色的「<b>锡躯</b>」牌。",

  ["lb_zero__wanwo_active"] = "万我",
  ["#lb_zero__wanwo-ask"] = "万我：请使用一张〖锡躯〗标记过的牌！",

  ["$lb_zero__wanwo1"] = "此时集火。",
  ["$lb_zero__wanwo2"] = "忠臣，果决。",
}

local wanwo_spec1 = {
  anim_type = "defensive",
  on_use = function(self, event, target, player, data)
    for i = 0, 110, 1 do
      local name = i == 0 and "lb_zero__xiqu" or "lb_zero" .. i .. "__xiqu"
      if not player:hasSkill(name, true) then
        player:setSkillUseHistory(name, 0, Player.HistoryGame)
        player.room:handleAddLoseSkills(player, name, nil, true, false)
        break
      end
    end
  end,
}

skel:addEffect(fk.RoundStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name)
  end,
  on_use = wanwo_spec1.on_use,
})

skel:addEffect(fk.Damaged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_use = wanwo_spec1.on_use,
})

local wanwo_spec2 = {
  anim_type = "defensive",
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = {}
    for _, p in ipairs(player.room:getAllPlayers()) do
      if not p:isKongcheng() then
        for _, id in ipairs(p:getCardIds("h")) do
          if Fk:getCardById(id):getMark("@@lb_zero__xiqu") ~= 0 and #Fk:getCardById(id, true):getDefaultTarget(player, { bypass_times = true }) > 0 then
            table.insertIfNeed(mark, id)
          end
        end
      end
    end
    if #mark > 0 then
      local wanwo = false
      for i = 0, 110, 1 do
        local name = i == 0 and "lb_zero__xiqu" or "lb_zero" .. i .. "__xiqu"
        if player:hasSkill(name, true) and player:usedSkillTimes(name, Player.HistoryGame) == 0 then
          room:handleAddLoseSkills(player, "-" .. name, nil, true, false)
          wanwo = true
          break
        end
      end
      if wanwo and #table.filter(mark, function(id) return table.find(room.alive_players,
              function(p) return table.contains(p:getCardIds("h"), id) end) end) > 0 then
        room:askToUseRealCard(player, {
          pattern = table.filter(mark,
            function(id) return table.find(room.alive_players,
                function(p) return table.contains(p:getCardIds("h"), id) end) end),
          skill_name = skel.name,
          prompt = "#lb_zero__wanwo-ask",
          extra_data = {
            bypass_times = true,
            extraUse = true,
            expand_pile = table.filter(mark,
              function(id) return table.find(room.alive_players,
                  function(p) return table.contains(p:getCardIds("h"), id) end) end),
          },
          cancelable = false,
        })
      end
    end
  end,
}

skel:addEffect(fk.RoundEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return end
    local mark = {}
    for _, p in ipairs(player.room:getAllPlayers()) do
      if not p:isKongcheng() then
        for _, id in ipairs(p:getCardIds("h")) do
          if Fk:getCardById(id):getMark("@@lb_zero__xiqu") ~= 0 and #Fk:getCardById(id, true):getDefaultTarget(player, { bypass_times = true }) > 0 then
            table.insertIfNeed(mark, id)
          end
        end
      end
    end
    for i = 0, 110, 1 do
      local name = i == 0 and "lb_zero__xiqu" or "lb_zero" .. i .. "__xiqu"
      if player:hasSkill(name, true) and player:usedSkillTimes(name, Player.HistoryGame) == 0 then
        return true
      end
    end
  end,
  on_use = wanwo_spec2.on_use,
})

skel:addEffect(fk.Damage, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(skel.name) and target == player) then return end
    local mark = {}
    for _, p in ipairs(player.room:getAllPlayers()) do
      if not p:isKongcheng() then
        for _, id in ipairs(p:getCardIds("h")) do
          if Fk:getCardById(id):getMark("@@lb_zero__xiqu") ~= 0 and #Fk:getCardById(id, true):getDefaultTarget(player, { bypass_times = true }) > 0 then
            table.insertIfNeed(mark, id)
          end
        end
      end
    end
    for i = 0, 110, 1 do
      local name = i == 0 and "lb_zero__xiqu" or "lb_zero" .. i .. "__xiqu"
      if player:hasSkill(name, true) and player:usedSkillTimes(name, Player.HistoryGame) == 0 and #mark > 0 then
        return true
      end
    end
  end,
  on_use = wanwo_spec2.on_use,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[lb_zero__xiqu]lb_zero__xiqu", 1)
end)

skel:addLoseEffect(function(self, player, is_death)
  if not player:hasSkill("lb_zero__yishan", true) then
    local function xiqu()
      for i = 0, 110, 1 do
        local skill = i == 0 and "lb_zero__xiqu" or "lb_zero" .. i .. "__xiqu"
        if player:hasSkill(skill, true) then
          return true
        end
      end
    end
    if not xiqu() then
      player.room:setPlayerMark(player, "@[lb_zero__xiqu]lb_zero__xiqu", 0)
    end
  end
end)

return skel
